// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 




using System;
using System.Collections;
using System.Net.Sockets;
using System.Threading;

namespace Hack.Net.Smtp
{
    public class SmtpServer : TcpServer
    {
        /** Set the SMTP properties */
        private readonly SmtpProperties prop;

        /** A delegate for received SMTP handler */
        public delegate void ReceivingHandler(SmtpReceiving recv);
        /** A SMTP request have been received */
        public event ReceivingHandler smtpReceived;


        /** Create a new SMTP server */
        public SmtpServer(SmtpProperties prop, int poolSize, int maxConn) :
            base(prop.port, poolSize, maxConn, null)
        {
            //Set SMTP properties and register the post-accept handler
            this.prop = prop;
            base.connhandler = new TcpServer.ConnectionHandler(this.HandleConnection);
        }


        /** Handle connection */
        private void HandleConnection(TcpClient client)
        {
            //Handle SMTP request
            SmtpReceiving recv = new SmtpReceiving(client, prop);

            //Notify registers
            if (smtpReceived != null)
                smtpReceived(recv);
        }
    }




}